package com.tianji.mq.produce;

import com.tianji.common.constant.MqConstants;
import com.tianji.coupon.service.LogSmsService;
import com.tianji.medical.mapper.MedicationSendLogMapper;
import com.tianji.medical.pojo.MedicationSendLog;
import com.tianji.medical.pojo.PatientMedication;
import com.tianji.medical.service.MedicationSendLogService;
import com.tianji.medical.service.PatientMedicationService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
@Slf4j
public class MedicationRemindProducer {

    private final RocketMQTemplate rocketMQTemplate;

    private final MedicationSendLogService medicationSendLogService;

    public void sendReminder(PatientMedication patientMedication) {
        try {
            rocketMQTemplate.convertAndSend(MqConstants.TOPIC, patientMedication);
            log.info("mq发送患者服药信息成功");
        }catch (Exception e){
            //原因：消息发送失败：网络异常、RocketMQ 服务不可用等。
            log.info("mq发送患者服药信息失败");
            //异步记录失败日志并且进入死信队列
            medicationSendLogService.logFailedSend(patientMedication,e.getMessage());
            rocketMQTemplate.convertAndSend(MqConstants.TOPIC_DLQ, patientMedication);
        }
    }
}
